package com.kimo3412.gradehub.controller;


import com.kimo3412.gradehub.entity.Grade;
import com.kimo3412.gradehub.entity.Student;
import com.kimo3412.gradehub.response.ApiResponse;
import com.kimo3412.gradehub.service.GradeService;
import com.kimo3412.gradehub.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private GradeService gradeService;

    @GetMapping("/listAll")
    public ResponseEntity<ApiResponse<List<Student>>> listAllStudents() {
        List<Student> students = studentService.findAllStudents();
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", students));
    }

    @GetMapping("/getById/{studentId}")
    public ResponseEntity<ApiResponse<Student>> getStudentById(@PathVariable Long studentId) {
        Student student = studentService.findStudentById(studentId);
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", student));
    }

    @GetMapping("/getGrades/{studentId}")
    public ResponseEntity<ApiResponse<List<Grade>>> getGradesByStudentId(@PathVariable Long studentId) {
        List<Grade> grades = gradeService.findGradesByStudentId(studentId);
        return ResponseEntity.ok(new ApiResponse<>(200, "Success", grades));
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Student>> addStudent(@RequestBody Student student) {
        Student savedStudent = studentService.addStudent(student);
        return ResponseEntity.ok(new ApiResponse<>(201, "Student added successfully", savedStudent));
    }
}
